var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;
var valide = libs.formValide;

var _page = {}

function clicks(cls, data){
    if (!cls){
        return false;
    }
    var rt = 'uc/edituserItem'
    if (cls === 'ui_edit_address')
        rt = 'uc/addresslist'

    if (cls === 'uc_edit_yqm'){
        var _val = $('#'+cls).val()
        if (_val){
            $('#'+cls).off('click')
            return false;
        }
    }
    $('#'+cls).click(function(){
        router(rt, {type: data})
    })
    return clicks;
}

function updateUserInfo(id, v){
    $('#'+id).val(v)
    return updateUserInfo
}

var bindEvent = function(){

    clicks('ui_edit_name', 'nickname')
        //   ('ui_edit_mobile', 'mobile')
          ('ui_edit_address', 'address')
          ('uc_edit_yqm', 'yqm')

}


//演示模块
var Show = React.createClass({
    mixins: [ItemMixin],
    componentWillMount: function() {
        var _user = SA.get('USER')
        this.setState({
            info: _user.info
        })
        console.log('========== 222');
        console.log(_user);
    },
    render:function(){
        var info = this.state.info;
        // var address = info.address ? (info.address.cityName + info.address.districtName + info.address.communityName)||'' : ''
        var address = info.address ? info.address.fullAddress ||'' : ''
        return (
            <div className="default_div app_index">
                <ul className="default_Picture">
                    <li className="dP_pic">
                        <label>头像</label>
                        <div className="dp_pic_r">
                            <img src="./images/ic_default_head.png"/>
                            {/*<i className="iconfont icon-xiangyou"></i>*/}
                        </div>
                    </li>
                    <li>
                        <label>昵称</label>
                        <input id="ui_edit_name" type="text" defaultValue={info.userName} readOnly="readOnly"/>
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                    <li>
                        <label>账号</label>
                        <input id="ui_edit_mobile" type="text" defaultValue={info.mobile} readOnly="readOnly"/>
                        {/*<i className="iconfont icon-xiangyou"></i>*/}
                    </li>
                    <li>
                        <label>地址管理</label>
                        <input id="ui_edit_address" type="text" defaultValue={address} readOnly="readOnly"/>
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                    <li>
                        <label>邀请码</label>
                        <input id="uc_edit_yqm" type="text" defaultValue={info.invitationCode} readOnly="readOnly"/>
                        <i className="iconfont icon-xiangyou"></i>
                    </li>
                </ul>
            </div>
        );
    }
});

function renderHtml(){
    var me = this;
    var oUser = SA.get('USER');
    api.req('/userInfo', {login: oUser.login, method: 'get'}, function(ddd){
        var _resault = {login: oUser.login, info: ddd}
        SA.set('USER', _resault)
        // if (!ddd.address){
        //     router('uc/addresslist')
        // }
        me.main()
    })
}

function start(name){
    return pages.new({
        boot:function(){

        },
        goback: function(){
            router.cb=function(){
                router('uc/index')
            }
        },
        trigger:function(self, data){
            _page.community = data
            this.libs.changeTitle('个人资料');    //更改当前页面标题
            renderHtml.call(this)
        },
        main: function(){
            this.render(
                <Show itemDefaultMethod={bindEvent} />,
                document.getElementById(name)
            )
        },
        end: function(){
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}

module.exports = start;
